Micron Document
Livres et Wikis | Archives | Info


JavaScript syntax
part 11/43 Β· 161.3 KB total
layout: Wide Β· Narrow Β· Centered
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
(such as \n). The JavaScript standard allows the backquote character
(`, a.k.a. grave accent or backtick) to quote multiline literal
strings, as well as embedded expressions using the syntax
${expression}.cite-ref-12[12]

const greeting = "Hello, World!";
const anotherGreeting = 'Greetings, people of Earth.';
const aMultilineGreeting = `Warm regards,
John Doe.`
// Template literals type-coerce evaluated expressions and interpolate
them into the string.
const templateLiteral = `This is what is stored in anotherGreeting:
${anotherGreeting}.`;
console.log(templateLiteral); // 'This is what is stored in
anotherGreeting: 'Greetings, people of Earth.''
console.log(`You are ${Math.floor(age)=>18 ? "allowed" : "not allowed"}
to view this web page`);

Individual characters within a string can be accessed using the charAt
method (provided by String.prototype). This is the preferred way when
accessing individual characters within a string, because it also works
in non-modern browsers:

const h = greeting.charAt(0);

In modern browsers, individual characters within a string can be
accessed (as strings with only a single character) through the same
notation as arrays:

const h = greeting[0];

However, JavaScript strings are immutable:

greeting[0] = "H"; // Fails.

Applying the equality operator ("==") to two strings returns true, if
the strings have the same contents, which means: of the same length and
containing the same sequence of characters (case is significant for
alphabets). Thus:

const x = "World";
const compare1 = ("Hello, " + x == "Hello, World"); // Here compare1
contains true.
const compare2 = ("Hello, " + x == "hello, World"); // Here compare2
contains ...
// ... false since the ...
// ... first characters ...
// ... of both operands ...
// ... are not of the same case.

Quotes of the same type cannot be nested unless they are escaped.

let x = '"Hello, World!" he said.'; // Just fine.
x = ""Hello, World!" he said."; // Not good.
x = "\"Hello, World!\" he said."; // Works by escaping " with \"

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────